<template>
  <div class="myContailer">
    <el-row class="myTitleBorderBottom">
      <el-col :span="20" class="myLineHeight_40px">
        <span v-if="type=='add'">新增楼栋</span>
        <span v-if="type=='update'">编辑楼栋</span>
        <span v-if="type=='info'">楼栋详情</span>
      </el-col>
    </el-row>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span> 所属片区 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="district_selected">
            <el-select v-model="ruleForm.district_selected"
              @change="changeDistrictSelect"
              :loading="loading"
              :disabled="is_disabled"
              placeholder="请选择片区">
              <el-option
                v-for="item in district_list"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span> 所属小区 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="area_selected">
            <el-select v-model="ruleForm.area_selected"
              :loading="loading"
              :disabled="is_disabled"
              placeholder="请选择小区">
              <el-option
                v-for="item in area_list"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span> 楼栋名称 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="building_name">
            <el-input  v-model="ruleForm.building_name" :readonly="is_modifly" placeholder="请输入楼栋名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span>
          楼栋编号 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="building_no">
            <el-input v-model="ruleForm.building_no" placeholder="请输入楼栋编号" :readonly="is_modifly"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          楼栋用途 :
        </el-col>
        <el-col :span="7">
          <el-select v-model="ruleForm.building_type"
            :loading="loading"
            :disabled="is_disabled"
            placeholder="请选择用途">
            <el-option
              v-for="item in ldYtArr"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          备注 :
        </el-col>
        <el-col :span="7">
          <el-input type="textarea" :readonly="is_modifly" v-model="ruleForm.extra_desc" placeholder="备注"></el-input>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="10" :offset="3">
          <el-button v-if="type=='add'" @click="create('ruleForm')" class="myBgGreen">新 增</el-button>
          <el-button v-if="type=='update' && isShow(115)" @click="modify('ruleForm')" class="myBgGreen">保 存</el-button>
          <el-button @click="onBack" class="myBgGreen">返 回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<style scoped>
  .myContailer{
    background: #fff;
    padding: 10px;
  }
  .myTitleBorderBottom{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    border-bottom: 1px solid #dbdbdb;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top:2px;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myMarginTop_10px{
    margin-top: 10px;
  }
  .myVA_M{
    vertical-align: middle;
  }
</style>
<script>
  import { getDistrictList, getAreaList } from '@/api/area'
  import { getBuildingDetail, modifyBuilding, createBuilding } from '@/api/building'
  export default {
    data(){
      return{
        title: "楼栋管理",
        id: null,
        detail: {},

        type:'',
        is_modifly:true,
        is_disabled:false,

        district_list: [],
        district_loaded: false,
        district_selected: "",

        area_list: [],
        area_loaded: false,
        // area_selected: "",

       
        district_id:"",
        selected:"",

        area_id:"",
        selectedXq:"",

        building_name:"",
        building_no:"",
        extra_desc:"",

        building_type:"",
        selectedYt:"",
        ldYtArr:[
          {id:1,name:"住宅"},
          {id:2,name:"商用"},
          {id:3,name:"混合"},
          {id:4,name:"学校"}
        ],
        loading:false,

        ruleForm:{
          area_selected:''
        },
        rules: {
          district_selected: [
            {required: true, message: '请选择所属片区', trigger: 'change' }
          ],
          area_selected:[
            {required: true, message: '请选择所属小区', trigger: 'change'}
          ],
          building_name:[
            {required: true, message: '请输入楼栋名称', trigger: 'blur'}
          ],
          building_no:[
            {required: true, message: '请输入楼栋编号', trigger: 'blur'}
          ]
        }
      }
    },
    components: {
        
    },
    mounted(){
      var that = this;
      this.type = this.$route.query.type;
      this.id = this.$route.query.id;

      if(this.type=='info'){ //查看
        this.is_modifly = true;
        this.is_disabled = true;
        this.fetchDetail();
      }

      if(this.type=='update'){ //修改
        this.is_modifly = false;
        this.fetchDetail();
      }

      if(this.type=='add'){
        this.is_modifly = false;
        this.clickDistrictSelect();
        //this.clickAreaSelect();
      }
      
    },
    methods:{
      fetchDetail() {
        let vm = this;
        this.loading = true;

        getBuildingDetail(this.id)
        .then(response => {
          vm.ruleForm = response.data;

          vm.district_list.push(vm.ruleForm.district_info);
          vm.ruleForm.district_selected = vm.ruleForm.district_info.value;

          vm.area_list.push(vm.ruleForm.area_info);
          vm.ruleForm.area_selected = vm.ruleForm.area_info.value;

          // console.log(vm.ruleForm)
          vm.loading = false;
        })
        .catch(error => {
          vm.loading = false;
        })
      },
      clickDistrictSelect() {
        let vm = this;
        if (!this.district_loaded) {
          getDistrictList()
          .then(response => {
            vm.district_list = response.data;
            vm.district_loaded = true;
          })
          .catch(error => {

          })
        }
      },
      changeDistrictSelect() {
        this.area_loaded = false;
        this.ruleForm.area_selected = '';
        this.area_list = [];
        this.clickAreaSelect()
      }, 
      clickAreaSelect(){
        let vm = this;
        if (this.ruleForm.district_selected != -1 && !this.area_loaded) {
          let params = {
            district_id: this.ruleForm.district_selected,
            ril: 'S',
            page_num: 1,
            page_size: 999
          };
          getAreaList(params)
          .then(response => {
            vm.area_list = response.data.rows;
            // console.log(vm.area_list);
            vm.area_loaded = true;
          })
          .catch(error => {

          })
        }
      },  
      modify(formName){
        let vm = this;
        vm.$refs[formName].validate((valid) => {//验证
          if (valid) {
            vm.loading = true;
            modifyBuilding(vm.id, vm.ruleForm)
            .then(response => {
              vm.$message.success("执行成功！");
              vm.loading = false;
              vm.onBack();
            })
            .catch(error=> {
              vm.loading = false;
            });
          } else {
            vm.$message.error("执行失败！");
            return false;
          }
        });
      },
      create(formName){
        let vm = this;
        vm.$refs[formName].validate((valid) => {//验证
          if (valid) {
            vm.ruleForm.area_id = vm.ruleForm.area_selected;
            vm.loading = true;
            createBuilding(vm.ruleForm)
            .then(response => {
              vm.$message.success("执行成功！");
              vm.loading = false;
              vm.onBack();
            })
            .catch(error=> {
              vm.loading = false;
            });
          } else {
            vm.$message.error("执行失败！");
            return false;
          }
        });    

      },
      onBack(){
        let vm=this;
        window.localStorage.setItem('pageNum',vm.$route.query.pageNum||1);
        vm.$router.go(-1);
      }

    }
  
  }


</script>



